package Command;

import debuggen.Debug;

/**
 * CommandConverter
 * @author Danny
 *
 *	Simple class which turns a string into a command.
 *	If the data was incorrect, we return a noCommand.
 *
 *	Declaration: 
 *  @see Resources.protocol.pdf
 */
public class CommandConverter {
	
	/**
	 * String2Command
	 * Turns a string into a command
	 * @param p_strCommand
	 * @return Command
	 */
	public static Command string2Command(String p_strCommand)
	{
		Command cmd = new NoCommand();
		
		try 
		{
			// Check if line is conform protocol
			if(p_strCommand.charAt(0) != '#' && p_strCommand.charAt(p_strCommand.length() -1) != '!')
			{
				// If in debug mode, write info to log 
				if(Debug.DEBUGGEN)
				{
					Debug.writeMessage("CommandConverter string2Command String contains errors (" + p_strCommand +")");
				}
				
				//String is incorrect, zo return a NoCommand
				return new NoCommand();
			}
			
			// Remove first and last char (# & !)
			p_strCommand = p_strCommand.substring(1, p_strCommand.length() - 1);
			// Split the string by ,
			String[] tmp = p_strCommand.split(",");
			// Create 
			int[] arrCommand = StringArrToIntArr(tmp);
			
			/**
			 * Switch where we are going to determine which command needs
			 * to be created. 
			 * No comment needed in switch, names says it all :)
			 */
			switch(arrCommand[0])
			{
				case 0:
						// Emergency stop
						if(arrCommand[1] == 0)
						{
							cmd = new EmergencySituation();
						}else if(arrCommand[1] == 1)
						{
							cmd = new EmergencyStop();
						} 
						break;
				case 1:
						// Engine
						if(arrCommand[1] == 0)
						{
							cmd = new EngineBackward();
						}else if(arrCommand[1] == 1)
						{
							cmd = new EngineOut();
						} else if(arrCommand[1] == 2)
						{
							cmd = new EngineForward();
						}
						break;
				case 2: 
						//Sail
							cmd = new Sail();
							cmd.setValue(arrCommand[1]);
						break;
				case 3:
						// Rudder
							cmd = new Rudder();
							cmd.setValue(arrCommand[1]);
						break;
				case 4:
						//Keel
						if(arrCommand[1] == 0)
						{
							cmd = new KeelUp();
						}else if(arrCommand[1] == 1)
						{
							cmd = new KeelDown();
						}
						break;
				case 5: 
						// Sensor gradient
						cmd = new Gradient();
						cmd.setValue(arrCommand[1]);
						break;
				case 6:
						// Direction of the wind
						cmd = new WindDirection();
						cmd.setValue(arrCommand[1]);
						break;
				case 7:
						// Droogvallen
						if(arrCommand[1] == 0)
						{
							cmd = new DroogvallenSailing();
						}else if(arrCommand[1] == 1)
						{
							cmd = new DroogvallenDry();
						}
						break;
				default:
						// If anything did go wrong, do nothing cause a NoCommand alreay exists
						break;
			}
			// Return command
			return cmd;
		}catch(Exception exc){
			return cmd;
		}
	}
	/**
	 * StringArrToIntArr
	 * @param arrSplitted
	 * @return
	 */
	private static int[] StringArrToIntArr(String[] arrSplitted)
	{
		// Create new array of type int
		int[] result = new int[arrSplitted.length];
		// Loop through array
		for(int i = 0; i < result.length; i++)
		{
			result[i] = Integer.parseInt(arrSplitted[i]);
		}
		// Return result
		return result;
	}
}
